package com.ruoyi.web.controller.systembasic;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mes.systembasic.domain.SmesMaterial;
import com.ruoyi.mes.systembasic.service.ISmesMaterialService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 原料管理Controller
 *
 * @author simpsense
 * @date 2020-11-27
 */
@RestController
@RequestMapping("/systembasic/material")
public class SmesMaterialController extends BaseController
{
    @Autowired
    private ISmesMaterialService smesMaterialService;

    /**
     * 查询原料管理列表
     */
    @PreAuthorize("@ss.hasPermi('systembasic:material:list')")
    @GetMapping("/list")
    public TableDataInfo list(SmesMaterial smesMaterial)
    {
        startPage();
        List<SmesMaterial> list = smesMaterialService.selectSmesMaterialList(smesMaterial);
        return getDataTable(list);
    }

    /**
     * 导出原料管理列表
     */
    @PreAuthorize("@ss.hasPermi('systembasic:material:export')")
    @Log(title = "原料管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SmesMaterial smesMaterial)
    {
        List<SmesMaterial> list = smesMaterialService.selectSmesMaterialList(smesMaterial);
        ExcelUtil<SmesMaterial> util = new ExcelUtil<SmesMaterial>(SmesMaterial.class);
        return util.exportExcel(list, "material");
    }

    /**
     * 获取原料管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('systembasic:material:query')")
    @GetMapping(value = "/{materialNo}")
    public AjaxResult getInfo(@PathVariable("materialNo") String materialNo)
    {
        return AjaxResult.success(smesMaterialService.selectSmesMaterialById(materialNo));
    }

    /**
     * 新增原料管理
     */
    @PreAuthorize("@ss.hasPermi('systembasic:material:add')")
    @Log(title = "原料管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SmesMaterial smesMaterial)
    {
        smesMaterial.setCreateBy(SecurityUtils.getUsername());
        return toAjax(smesMaterialService.insertSmesMaterial(smesMaterial));
    }

    /**
     * 修改原料管理
     */
    @PreAuthorize("@ss.hasPermi('systembasic:material:edit')")
    @Log(title = "原料管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SmesMaterial smesMaterial)
    {
        smesMaterial.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(smesMaterialService.updateSmesMaterial(smesMaterial));
    }

    /**
     * 删除原料管理
     */
    @PreAuthorize("@ss.hasPermi('systembasic:material:remove')")
    @Log(title = "原料管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{materialNos}")
    public AjaxResult remove(@PathVariable String[] materialNos)
    {
        return toAjax(smesMaterialService.deleteSmesMaterialByIds(materialNos));
    }
}